package lium.lbs.lbsim.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.buz.hjcdriver.bean.SelectBankListResultBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lmlibrary.bean.ResponseBean
import lium.lbs.lbsim.utils.callbck.DialogCallback
import lium.lbs.lbsim.utils.ToastUtils
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_banklist.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.adapter.SelectBankListAdapter
import java.util.HashMap

class SelectBankListActivity : BaseActivity(), BaseQuickAdapter.OnItemClickListener {

    lateinit var adapter: SelectBankListAdapter
    var page:Int=1

    override fun getLayoutId(): Int { return R.layout.activity_banklist  }

    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("选择银行")
        initRecyclerView()
        getDataList()
    }

    fun initRecyclerView(){
        //初始化adapter
        adapter = SelectBankListAdapter()
        val layoutManager = LinearLayoutManager(this)
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = adapter
        recyclerView.addItemDecoration(DividerItemDecoration(this,layoutManager.orientation))
        adapter.onItemClickListener = this
        adapter.disableLoadMoreIfNotFullPage(recyclerView) //关闭BRAVE的自动加载功能 改成 refreshLayout的上拉加载
        refreshLayout.setOnRefreshListener {
            page =1
            getDataList()
        }
        refreshLayout.setOnLoadMoreListener {
            page =1
            getDataList()
        }
    }
    private fun getDataList() {
        val param = HashMap<String, String>()
        postData("/common/selectBank",param, object : DialogCallback<ResponseBean<List<SelectBankListResultBean>>>(this) {
            override fun onSuccess(response: Response<ResponseBean<List<SelectBankListResultBean>>>) {
                setDataList(response.body().data)
            }
        })
    }

    private fun setDataList(list: List<SelectBankListResultBean>) {
        if (page==1){
            adapter!!.data.clear()
        }
        refreshLayout.finishRefresh()
        refreshLayout.finishLoadMore()
        list?.let {
            adapter!!.data.addAll(list)
        }
        if (list==null || list.isEmpty()) {
            refreshLayout.finishLoadMoreWithNoMoreData()  //没有数据
        }
        refreshLayout.finishLoadMoreWithNoMoreData()  //没有数据
        adapter!!.notifyDataSetChanged()
    }
    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        adapter?.data!![position]?.let {
            var bean =adapter?.data!![position] as SelectBankListResultBean
            var backIntent = Intent()
            backIntent.putExtra("bean",bean)
            ToastUtils.showToast(bean.name)
            setResult(Activity.RESULT_OK,backIntent)
            finish()
        }
    }

    override fun getNetWorkData() {
    }


}